<?php
namespace app\sisome\controller;
use think\Request;
class Kuang extends Common{	//矿机后台
    public function index(){
    	// die('234');
        //return $this->fetch('hello',['name'=>'thinkphp']);
        //return view('hello',['name'=>'thinkphp']);
        // 方法    说明
        // fetch   渲染模板输出
        // display 渲染内容输出
        // assign  模板变量赋值
        // engine  初始化模板引擎
        return view();
    }
    public function xuexi(){    //学习中心题目-选择题
        $request = Request::instance();
        if($request->isPost()){
            $post = $request->param();
            $data['xuhao']=$post['xuhao'];
            $data['timu']=$post['timu'];
            $data['a']=$post['a'];
            $data['b']=$post['b'];
            $data['c']=$post['c'];
            $data['d']=$post['d'];
            $data['daan']=$post['daan'];
            $data['zt']=1;
            if($post['type']=='edit' && $post['id']){
                $ti=db('xuexi_xuanzeti')->where('id',$post['id'])->find();
                if(!$ti){
                    $this->error('查无此题');
                }else{
                    $ret=db('xuexi_xuanzeti')->where('id',$post['id'])->update($data);
                    if($ret){
                        $this->success('修改成功');
                    }else{
                        $this->error('修改失败');
                    }
                }
            }else{
                $ret=db('xuexi_xuanzeti')->insertGetId($data);
                if($ret){
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }
        }elseif($request->isGet() && isset($request->param()['type'])){
            $get = $request->param();
            if($get['type']=='del'){
                $ti=db('xuexi_xuanzeti')->where('id',$get['id'])->find();
                if(!$ti){
                    $this->error('查无此题');
                }else{
                    $ret=db('xuexi_xuanzeti')->where('id',$get['id'])->delete();
                    if($ret){
                        $this->success('删除成功');
                    }else{
                        $this->error('删除失败');
                    }
                }
            }
        }else{
            $lists = db('xuexi_xuanzeti')->order('xuhao asc')->paginate(10);
            $this->assign('lists', $lists);
            return view();
        }
    }

    public function tilian_list(){   //提链列表
        $param = request()->param();
        $where = [];
        if(isset($param['status'])){
            if($param['status']<>'all'){
                $where['status']=$param['status'];
            }
        }else{
            $where['status']=0;
        }

        $lists = db('tilian')->where($where)->order('id asc')->paginate(10);
        $sum['all'] = db('tilian')->where([])->sum('money');
        $sum['ok'] = db('tilian')->where('status',1)->sum('money');
        $sum['ing'] = db('tilian')->where('status',3)->sum('money');
        $max_nonce = db('tilian')->max('nonce+1');
        $this->assign('sum', $sum);
        $this->assign('max_nonce', $max_nonce);
        $this->assign('lists', $lists);
        $this->assign('status', isset($param['status'])?$param['status']:'0');
        return view();
    }
    public function tilian_do(){   //提链执行
        $param = request()->param();
        $where = [];
        //合法性
        if(!isset($param['id'])){
            $this->error('非法请求！');
        }
        $item = db('tilian')->where('id',$param['id'])->find();
        if(!$item){
            $this->error('不存在！');
        }elseif(!in_array($item['status'],[0,2])){
            $this->error('请勿重复处理！'.$item['status']);
        }
        //请求钱包接口
        //http://47.92.195.55:8870/?type=2&from=发送方地址&privateKey=发送方私钥&to=接收方地址&value=数量&contract=代币合同地址&omecnonce=自增值
        $sis_user=userinfobyid('1');
        $sis_qb=db('adeth')->where('id',$sis_user['aid'])->find();
        $user_qb=db('adeth')->where('uname',$item['uname'])->find();
        $omecnonce = db('tilian')->max('nonce+1');   //自增值
        $urlqb=config('coin_host').':8870';
        $params['type']=2;
        $params['from']=$sis_qb['address'];
        $params['privateKey']=$sis_qb['key'];
        $params['to']=$user_qb['address'];
        $params['value']=floatval($item['money']);  //去除小数点后多余0;
        $params['contract']=config('con_omec');
        $params['omecnonce']=$omecnonce;
        $retqb=http($urlqb,$params,'GET',30);
        // 自增数错误返回{"status":0,"hash":{}}
        // 正常返回{"status":1,"hash":"0x0814c160252eeae7b320e75bc2adf1b782f077cfd2a3ad7c573218243d85a140"}
        if(!$retqb){
            $this->error('接口失败');
        }
        $retqb=json_decode($retqb,true);
        if($retqb && $retqb['status']==1 && $retqb['hash']){
            $ret = db('tilian')->where('id',$item['id'])->update([
                'status'=>3,
                'uptime'=>time(),
                'hash'=>$retqb['hash'],
                'nonce'=>$omecnonce,
            ]);
            if($ret){
                $this->success('执行成功');
            }else{
                $this->error('提链成功，但写入失败!!');
            }
            $this->success('执行成功');
        }else{
            db('tilian')->where('id',$item['id'])->update([
                'status'=>2,
                'uptime'=>time(),
            ]);
            $this->error('提链失败');
        }
    }
    public function tilian_status(){    //提链结果手动查询
        $param = request()->param();
        $where = [];
        //合法性
        if(!isset($param['id'])){
            $this->error('非法请求！');
        }
        $item = db('tilian')->where('id',$param['id'])->find();
        if(!$item){
            $this->error('不存在！');
        }elseif($item['status']<>3){
            $this->error('请勿重复查询！');
        }

        //http://47.92.195.55:8887/?type=1&hash=0x9db9ed1c817973de13c157cff17aae5fd0f6652be4c126c30af0bc3750a58679
        $urlqb=config('coin_host').':8887';
        $params['type']=1;
        $params['hash']=$item['hash'];
        $ret_qb=http($urlqb,$params,'GET');
        $ret_qb=json_decode($ret_qb,true);
        // dump($ret_qb);
        // dump($ret_qb['hash']['status']);
        // dump($ret_qb['hash']['gasUsed']);
        // dump($ret_qb['hash']['logs'][0]['blockNumber']);
        // die();
        if($ret_qb && isset($ret_qb['hash']['status']) && $ret_qb['hash']['status']){
            db('tilian')->where('id',$item['id'])->update([
                'status'=>1,
                'blocknumber'=>$ret_qb['hash']['logs'][0]['blockNumber'],
                'gasused'=>$ret_qb['hash']['gasUsed']
            ]);
            $this->success('已到账');
        }else{
            $this->success('还未到账');
        }
    }

    public function tilian_nonce(){ //修正nonce
        $param = request()->param();
        if (!isset($param['nonce'])) {
            $this->error('请输入修正内容');
        }
        $param['nonce'] = intval($param['nonce']);
        if($param['nonce']<=0){
            $this->error('请输入正数');
        }
        $ret = db('tilian')->where('id','11')->update(['nonce'=>$param['nonce']]);
        if($ret){
            $this->success('修改成功，请调试提链');
        }else{
            $this->error('修改失败');
        }
    }
}
